﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text;
using System.Xml.Serialization;
using System.Xml;

namespace TradeTestWeb
{
    public class ObjectUtil
    {
        public static T GetObject<T>(string xml)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                byte[] bytes = Encoding.UTF8.GetBytes(xml);
                ms.Write(bytes, 0, bytes.Length);
                ms.Flush();
                ms.Position = 0;
                XmlSerializer xs = new XmlSerializer(typeof(T));
                return (T)xs.Deserialize(ms);
            }
        }

        public static string GetXml(object spin)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                var xmlSerializerNamespaces = new XmlSerializerNamespaces();
                xmlSerializerNamespaces.Add("", "");
                new XmlSerializer(spin.GetType()).Serialize(new XmlTextWriter(ms, Encoding.UTF8), spin);
                ms.Flush();
                ms.Position = 0;
                using (StreamReader sr = new StreamReader(ms))
                {
                    return sr.ReadToEnd();
                }
            }
        }
    }
}